<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button class="filter-item" type="success" icon="el-icon-edit" @click="handleSave(0)">
        添加
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row row-key="id" style="width: 100%;">
      <el-table-column label="名称" prop="name" align="center" />
      <el-table-column label="图片" align="center">
        <template slot-scope="{row}">
          <el-image style="width: 100px; height: 100px" v-if="row.image" :src="row.image"
            :preview-src-list="[row.image]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"/>
      <el-table-column label="操作" fixed="right" width="130px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleSave(row.id)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { articlePageByQo, newsRemoveByIds } from '@/api/website'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves' // waves directive

export default {
  name: "ZcyxArticleList",
  directives: { waves },
  props: {
    type: {
      default: 1,
      type: Number,
    },
  },
  components: {
    Pagination
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        gongyingId: '',
        pageNo: 1,
        pageSize: 20,
        code: ''
      },
    }
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  watch: {
    $route: {
      handler: function(route) {
        const t = route.query.t;
        if (t) {
          this.handleFilter()
        }
      },
      immediate: true
    }
  },
  methods: {
    init() {
      if (this.type == 1) {
        this.listQuery.code = 'aboutZcyx'
      } else if (this.type == 2) {
        this.listQuery.code = 'zcyxTeaKnowledge'
      }  else if (this.type == 3) {
        this.listQuery.code = 'zcyxFamousMaster'
      }  else if (this.type == 4) {
        this.listQuery.code = 'zcyxFamousMountain'
      }  else if (this.type == 5) {
        this.listQuery.code = 'zcyxVideo'
      }  else if (this.type == 6) {
        this.listQuery.code = 'zcyxManagement'
      }  else if (this.type == 7) {
        this.listQuery.code = 'zcyxJudgeExchange'
      }  else if (this.type == 8) {
        this.listQuery.code = 'zcyxIndustryInfo'
      }  else if (this.type == 9) {
        this.listQuery.code = 'zcyxFamousProduct'
      } 
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    getList() {
      this.listLoading = true

      this.listQuery.gongyingId = this.userInfo.id
      articlePageByQo(this.listQuery).then(response => {
        this.list = response.list || []
        this.total = response.totalCount
        this.listLoading = false
      }).catch((err) => {
        console.log(err)
        this.listLoading = false
      })
    },
    handleDelete(row) {
      this.$confirm('是否继续删除？').then(() => {
        newsRemoveByIds({ ids: row.id }).then(() => {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.handleFilter()
        })
      })
    },
    handleSave(id) {
      if (this.type == 1) {
        this.$router.push({
          path: "/zcyx/zcyxArticleList/aboutZcyxSave",
          query: {
            id: id
          }
    	  });
      } else if (this.type == 2) {
        this.$router.push({
          path: "/zcyx/zcyxArticleList/teaKnowledgeSave",
          query: {
            id: id
          }
    	  });
      } else if (this.type == 3) {
        this.$router.push({
          path: "/zcyx/zcyxArticleList/famousMasterSave",
          query: {
            id: id
          }
    	  });
      } else if (this.type == 4) {
        this.$router.push({
          path: "/zcyx/zcyxArticleList/famousMountainSave",
          query: {
            id: id
          }
    	  });
      } else if (this.type == 5) {
        this.$router.push({
          path: "/zcyx/zcyxArticleList/zcyxVideoSave",
          query: {
            id: id
          }
    	  });
      } else if (this.type == 6) {
        this.$router.push({
          path: "/zcyx/zcyxArticleList/articleManagementSave",
          query: {
            id: id
          }
    	  });
      } else if (this.type == 7) {
        this.$router.push({
          path: "/zcyx/zcyxArticleList/judgeExchangeSave",
          query: {
            id: id
          }
    	  });
      } else if (this.type == 8) {
        this.$router.push({
          path: "/zcyx/zcyxArticleList/industryInfoSave",
          query: {
            id: id
          }
    	  });
      } else if (this.type == 9) {
        this.$router.push({
          path: "/zcyx/zcyxArticleList/famousProductSave",
          query: {
            id: id
          }
    	  });
      }
    }
  },
  created() {
    this.init()
    this.getList()
  },
  mounted() {
    
  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>
  
</style>